package mailServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.MailSendDAO;
import dto.MailSendDTO;
import dto.UserDTO;

@SuppressWarnings("serial")
public class MailReadyServlet extends HttpServlet {

	public MailReadyServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.execute(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.execute(request, response);

	}
	public void execute(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		List<MailSendDTO> list=new ArrayList<MailSendDTO>();
		String currentpg=request.getSession().getAttribute("ccpg")==null?"1":request.getSession().getAttribute("ccpg").toString();
		String cpage=request.getParameter("currentpage")==null?currentpg:request.getParameter("currentpage");
		int currentpage=Integer.parseInt(cpage);
		MailSendDAO mdao=new MailSendDAO();
		UserDTO udto=(UserDTO) request.getSession().getAttribute("USER");
		String userName=udto.getUserName();
//		list=mdao.chekReceive(receiveName);
		
		int count=mdao.ReadyCount(userName);
		int lastpage=1;
		if(count%10==0){
			lastpage=count/10;
		}else{
			lastpage=count/10+1;
		}
		if(currentpage<1){
			currentpage=1;
		}
		if(currentpage>lastpage){
			currentpage=lastpage;
		}
		list=mdao.HaveReady(userName, currentpage);
		request.getSession().setAttribute("ccpg", currentpage);
		request.setAttribute("currentpage", currentpage);
		request.setAttribute("lastpage", lastpage);
		request.setAttribute("list", list);
		request.getSession().setAttribute("right", "MailReadyServlet");
		request.getRequestDispatcher("mailJsp/ready.jsp").forward(request, response);
	    
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
